package javbot.gui;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import javbot.GlobalClass;

public class MailCheckPanel extends JPanel {

	private static final long serialVersionUID = 4722673037336539139L;
	private JTextField mailInServerTextField = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JTextField loginTextField = null;
	private JPasswordField passwordField = null;
	private JButton saveButton = null;
	private JLabel jLabel4 = null;
	private JTextField intervalTextField = null;
	private int checkInterval;

	/**
	 * This method initializes mailInServerTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMailInServerTextField() {
		if (mailInServerTextField == null) {
			mailInServerTextField = new JTextField();
			mailInServerTextField.setBounds(new java.awt.Rectangle(121,45,166,20));
		}
		return mailInServerTextField;
	}

	/**
	 * This method initializes loginTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getLoginTextField() {
		if (loginTextField == null) {
			loginTextField = new JTextField();
			loginTextField.setBounds(new java.awt.Rectangle(121,74,166,20));
		}
		return loginTextField;
	}

	/**
	 * This method initializes passwordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setBounds(new java.awt.Rectangle(121,104,166,20));
		}
		return passwordField;
	}

	/**
	 * This method initializes okButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkButton() {
		if (saveButton == null) {
			saveButton = new JButton();
			saveButton.setBounds(new java.awt.Rectangle(207,141,82,23));
			saveButton.setText("Save");

			saveButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						GlobalClass.mailProps.setCheckInterval(checkInterval);
						GlobalClass.mailProps.setMailServerIn(mailInServerTextField.getText());
						GlobalClass.mailProps.setLogin(loginTextField.getText());
						if (passwordField.getPassword().length > 0)
							GlobalClass.mailProps.setPass(passwordField.getPassword());
						GlobalClass.mailProps.saveMailProps();
						GlobalClass.stopMailChecker();
						GlobalClass.startMailChecker();
						close();
						
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return saveButton;
	}

	/**
	 * This is the default constructor
	 */
	public MailCheckPanel() {
		super();
		initialize();
		checkInterval = GlobalClass.mailProps.getCheckInterval();
		intervalTextField.setText(Integer.toString(checkInterval));
		mailInServerTextField.setText(GlobalClass.mailProps.getMailServerIn());
		loginTextField.setText(GlobalClass.mailProps.getLogin());
//		passwordField.setText(GlobalClass.mailProps.getPass());		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel4 = new JLabel();
		jLabel4.setBounds(new java.awt.Rectangle(14,18,185,16));
		jLabel4.setText("Check Time Interval (minutes) :");
		jLabel3 = new JLabel();
		jLabel3.setBounds(new java.awt.Rectangle(15,106,98,16));
		jLabel3.setText("Password:");
		jLabel2 = new JLabel();
		jLabel2.setBounds(new java.awt.Rectangle(15,76,98,16));
		jLabel2.setText("Login:");
		jLabel1 = new JLabel();
		jLabel1.setBounds(new java.awt.Rectangle(15,47,98,16));
		jLabel1.setText("POP Mail Server:");
		this.setLayout(null);
		this.setSize(301, 179);
		this.add(getMailInServerTextField(), null);
		this.add(jLabel1, null);
		this.add(jLabel2, null);
		this.add(jLabel3, null);
		this.add(getLoginTextField(), null);
		this.add(getPasswordField(), null);
		this.add(getOkButton(), null);
		this.add(jLabel4, null);
		this.add(getIntervalTextField(), null);
	}
	
	public void close() {
		((JDialog) getParent().getParent().getParent()).dispose();
	}

	/**
	 * This method initializes intervalTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getIntervalTextField() {
		if (intervalTextField == null) {
			intervalTextField = new JTextField("10");
			intervalTextField.setBounds(new java.awt.Rectangle(207,17,39,20));		
			
			intervalTextField.setInputVerifier(new InputVerifier() {

				public boolean verify(JComponent input) {
					String text = ((JTextField) input).getText();
					boolean ret = false;
										
					try {
						int i = Integer.parseInt(text);
						if (!(i < 0 || i > 999))  {
							checkInterval = i;
							ret = true;
						}
					} catch (NumberFormatException e) {}
					
					return ret;
				}
				
			});
		}
		return intervalTextField;
	}
	

}  //  @jve:decl-index=0:visual-constraint="8,11"
